﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DAL.Entity;
using DAL.Persistencia;
using System.IO;
using Microsoft.SqlServer.Server;

namespace NewTierra.Pages
{
    public partial class teste : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AcertaNoites();
                //AddFotos();
                //BackupDatabase();
            }
        }

        protected void AddFotos()
        {
            try
            {
                NewTierraEntities Con = new NewTierraEntities();

                List<Supplier> lista = Con.Suppliers.Where(s => s.TIPOSUPL_id == 1).ToList();

                foreach (Supplier item in lista)
                {
                    CriarPastaSupplier(item.S_id);

                    string pathOrig = ("~/Galeria/semimagem.jpg");
                    string pathDest = ("~/Galeria/" + item.S_id + "/semimagem.jpg");

                    File.Copy(Server.MapPath(pathOrig), Server.MapPath(pathDest), true);

                    SalvarFoto("semimagem.jpg", item.S_id);
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        public bool SalvarFoto(string ImgNome, int IdSupplier)
        {
            S_Image s = new S_Image();

            try
            {
                s.Image_capa = "S";
                s.Image_nome = ImgNome;
                s.Image_hupload = Convert.ToString(DateTime.Now);
                s.S_id = IdSupplier;

                FotoDAL f = new FotoDAL();

                f.Salvar(s);
                return true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return false;
            }
        }

        protected void CriarPastaSupplier(int IdSupplier)
        {
            string path = ("~/Galeria/" + IdSupplier);
            try
            {
                if (!Directory.Exists(Server.MapPath(path)))
                { Directory.CreateDirectory(Server.MapPath(path)); }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        public static void BackupDatabase()
        {

            try
            {
                SqlConnection sqlConnection1 = new SqlConnection("Data Source=(local);Initial Catalog=4S;Integrated Security=True;MultipleActiveResultSets=True");
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader;

                cmd.CommandText = "sp_backup";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();

                reader = cmd.ExecuteReader();

                sqlConnection1.Close();
            }
            catch
            {
                throw;
            }

        }

        protected void AcertaNoites()
        {
            try
            {
                MinimoNoiteDAL mnd = new MinimoNoiteDAL();
                for (int i = 0; i < 100; i++)
                {
                    int LastQtd = mnd.RetornaLast() + 1;
                    
                    MinimoNoites mn = new MinimoNoites();
                    mn.MinimoNoites_qtd = 1;
                    mnd.Salvar(mn);
                }

                Response.Write("Foi ok");

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

    }
}